﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/* Serialisation and files processing. */
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;

/* Work with XML. */
using System.Xml.Linq;
using System.Xml;
using System.Xml.Serialization;
/* Serial port. */
using System.IO.Ports;


namespace InterferometerGUI
{
    public partial class MainForm : Form
    {
        public Graph_2D Graph_2D_A;

        #region Serial potr variables
        
        /*------- Serial communication  port ----*/
        Communication.LineSerialPort Port_1;
            //class Port
            //SeriaPortCommunication.SerialCommunication Port_1;

            const int ROUND_BUFFER_SIZE = 128;/* Round buffer size. */
            string CurrentComPortName;
        #endregion

        public MainForm()
        {
            InitializeComponent();
        }


        private void button4_Click(object sender, EventArgs e)
        {

            Graph_2D_A = new Graph_2D(
                                     new string[] {  
                                                                    "X", 
                                                                    "Y", 
                                                                    "Z", 
                                                                 },
                                     new Color[]  {
                                                                    
                                                                    Color.Black,
                                                                    Color.Red,
                                                                    Color.Blue,
                                                                 },
                                                 " Line position. ",
                                                 " Samples. ",
                                                 " Value of Line positios,Dots."
                                   );
            /* We should give different name for each form to make possible save form parameters to file.*/
            Graph_2D_A.Text = "Position of interferention line.";
            // Graph_2D_A.MinimizeInsteadOfClose = true;
            Graph_2D_A.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            #region Load Form Parameters from setup file.
            /* Load setup parameters for this form. */
            FormSetup.SFP my_setup = new FormSetup.SFP();
            FormSetup.SFP.FormParameters sfp = new FormSetup.SFP.FormParameters();
            /* Set setup file name. */

            my_setup.SetupFileFileName = "setup.xml";
            /* Read setup file. */
            sfp = my_setup.ReadSetupFile(this);
            if (sfp != null)
            {
                this.Left = sfp.FormPosition_Left;
                this.Top = sfp.FormPosition_Top;
                this.Height = sfp.FormHeight;
                this.Width = sfp.FormWidth;
                this.CurrentComPortName = sfp.FormComPortName;
            }
            #endregion

            #region Scan current Com ports and set Com Port name.
            /* Scan all serial ports. */
            try
            {
                /* Get list of avialabled com-ports.*/
                string[] availablePorts = SerialPort.GetPortNames();
                /* Add list of com-ports to combo box. */
                int PortIndex = 0;
                foreach (string port in availablePorts)
                {
                    if (this.CurrentComPortName == port)
                    {
                        PortIndex = comboBox1.Items.Count;
                    }
                    comboBox1.Items.Add(port);
                }
                comboBox1.Text = comboBox1.Items[PortIndex].ToString();
            }
            catch (Exception e9)
            {
                MessageBox.Show(e9.Message);
            }
            #endregion

            Port_1 = new Communication.LineSerialPort(new Communication.LineSerialPort.MyCallDelegate(send_data_to_graph));
//            Port_1 = new SeriaPortCommunication.SerialCommunication();
            //Port_1 = new Communication.LineSerialPort();

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            #region Save current Form Parameters to setup file.

            FormSetup.SFP my_setup = new FormSetup.SFP();
            my_setup.SetupFileFileName = "setup.xml";
            my_setup.ComPortName = CurrentComPortName;
            my_setup.WriteSetupFile(this);

            #endregion

            #region Close opened file.

            #endregion
        }

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            CurrentComPortName = comboBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int result;
            result = Port_1.ComPort(
                                        comboBox1.Text,
                                        StopBits.One,
                                        8,
                                        115200,
                                        Parity.None,
                                        ROUND_BUFFER_SIZE
                                    );


            if (result == 0)
            {
                button1.Enabled = false;
                button1.Visible = false;
                button2.Visible = true;
                button3.Enabled = true;
                button4.Enabled = true;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (Port_1.ComPortClose() == 0)
                {
                    if (Port_1.serialComPort.IsOpen)
                    {
                        Port_1.serialComPort.Close();
                    }
                    button2.Enabled = false;
                    button1.Enabled = true;
                    button2.Visible = false;
                    button1.Visible = true;
                    button3.Enabled = false;
                    button4.Enabled = false;
                }
            }
            catch (Exception e2)
            {
                MessageBox.Show(e2.Message);
            }
        }

        public void send_data_to_graph(double x, double y,double z)
        {

           textBox4.Invoke(new Action(() => { textBox4.Text = Convert.ToString(x); }));
           textBox5.Invoke(new Action(() => { textBox5.Text = Convert.ToString(y); }));
           textBox6.Invoke(new Action(() => { textBox6.Text = Convert.ToString(z); }));
            if (Graph_2D_A != null)
            {
                Graph_2D_A.DrawGraph(
                                        x,
                                        y,
                                        0,
                                    300.0,  /* Scale min. */
                                       10, /* Scale max. */
                                     60000  /* Max amount of point. */
                                   );
                Graph_2D_A.DrawGraph(
                                        x,
                                        z,
                                        1,
                                    300.0,  /* Scale min. */
                                       10, /* Scale max. */
                                     60000  /* Max amount of point. */
                                    );
            }
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            double Wh = 0, Kh =0,Dt = 0;
            double Lp = 0;

            Lp = Convert.ToDouble(textBox3.Text);
            Dt = Convert.ToDouble(textBox1.Text);
            Kh = 300000000.0 * 650 / (1000000000.0 * Lp);
            Wh = Kh * Dt;
            textBox2.Text = Convert.ToString(Wh);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Port_1.ComPortClose();
            button2.Enabled = true;
            button2.Visible = true;
            button3.Enabled = false;
            //button3.Visible = false;
            button4.Enabled = false;
            //button4.Visible = false;


        }

        private void button5_Click(object sender, EventArgs e)
        {
            Port_1.read_byte_counter = 0;
        }
    }
}
